<template>
  <div class="main">
    <!-- 搜索框 -->
    <div class="search">
      <input
        type="text"
        placeholder="输入联系方式查询"
        v-model="searchKey"
        @keyup.enter="search"
      />
      <img src="../assets/images/serach.png" alt="" srcset="" @click="search" />
    </div>
    <!-- 工单递交 -->
    <div class="word-order" v-if="isShow">
      <div class="title">工单递交</div>
      <div class="row-input">
        <div class="lf">产品</div>
        <div class="md">:</div>
        <el-select v-model="product" placeholder="请选择">
            <el-option
              v-for="item in productOptions"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
      </div>
      <div class="row-textarea">
        <div class="lf">产品问题</div>
        <div class="md">:</div>
        <textarea
          name=""
          id=""
          cols="30"
          rows="10"
          class="rt"
          v-model="product_issue"
        ></textarea>
      </div>
      <div class="row-textarea">
        <div class="lf">温馨提示</div>
        <div class="md">:</div>
        <div class="rt" v-html="remind"></div>
      </div>
      <div class="row-textarea">
        <div class="lf">描述问题</div>
        <div class="md">:</div>
        <textarea
          name=""
          id=""
          cols="30"
          rows="10"
          class="rt"
          v-model="detail"
        ></textarea>
      </div>
      <div class="row-textarea">
        <div class="lf">添加附件</div>
        <div class="md">:</div>
        <div class="upload">
          <!-- <el-upload
            ref="uploadImg"
            action="http://wudao.cqlink.club/api/upload"
            :headers="{'Content-Type': 'application/x-www-form-urlencoded'}"
            list-type="picture-card"
            :limit="4"
            accept="image/gif, image/jpeg, image/png"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemoveImg"
            :on-success="handleAvatarSuccessImg"
            :on-error="handleAvatarError"
            :on-exceed="handleExceedImg"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog> -->
          <el-upload
            ref="uploadImg"
            action="#"
            list-type="picture-card"
            :limit="4"
            accept="image/gif, image/jpeg, image/png"
            :on-preview="handlePictureCardPreview"
            :on-exceed="handleExceedImg"
            :http-request="uploadImg"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </div>
      </div>
      <div class="row-textarea upload-file">
        <div class="lf">机密文件</div>
        <div class="md">:</div>
        <!-- <el-upload
          multiple
          action="http://wudao.cqlink.club/api/upload"
          :headers="{'Content-Type':'application/x-www-form-urlencoded'}"
          :on-success="handleAvatarSuccessFile"
          :on-error="handleAvatarError"
          :on-remove="handleRemoveFile"
          :show-file-list="true"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload> -->
        <el-upload
          multiple
          action="#"
          :show-file-list="true"
          :http-request="uploadFile"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </div>
      <div class="row-input">
        <div class="lf">联系方式</div>
        <div class="md">:</div>
        <input type="text" class="rt" v-model="phone" maxlength="11" />
      </div>
      <!-- <div class="row-email">
        <div class="lf">邮箱验证</div>
        <div class="md">:</div>
        <input type="text" class="rt" />
        <span>获取验证码</span>
      </div> -->
      <div class="row-security-code">
        <div class="lf">验证码</div>
        <div class="md">:</div>
        <input type="text" class="rt" v-model="code" />
        <img :src="codeImg" alt="" srcset="" @click="refresh" class="code-img" />
      </div>
      <div class="submit-btn" @click="submite">提交</div>
    </div>
    <!-- 查询结果 -->
    <template v-else>
      <div class="search-result" v-for="(item, index) in workData" :key="index">
        <div class="row">
          <div class="lf">产品</div>
          <div class="md">:</div>
          <div class="rt">{{ item.product }}</div>
        </div>
        <div class="row">
          <div class="lf">产品问题</div>
          <div class="md">:</div>
          <div class="rt">{{ item.product_issue }}</div>
        </div>
        <!-- <div class="row">
          <div class="lf">温馨提示</div>
          <div class="md">:</div>
          <div class="rt" v-html="item.remind"></div>
        </div> -->
        <div class="row">
          <div class="lf">描述问题</div>
          <div class="md">:</div>
          <div class="rt">
            {{ item.detail }}
          </div>
        </div>
        <div class="row" v-if="item.files != ''">
          <div class="lf">添加附件</div>
          <div class="md">:</div>
          <div class="rt">
            <img
              v-for="(items, index) in item.files"
              :key="index"
              :src="items"
              alt=""
            />
          </div>
        </div>
        <div class="row" v-if="item.secret_files != ''">
          <div class="lf">机密文件</div>
          <div class="md">:</div>
          <div class="rt">
            <a
              :href="item"
              v-for="(items, index) in item.secret_files"
              :key="index"
              :src="items"
              >{{ items }}</a
            >
          </div>
        </div>
        <div class="row">
          <div class="lf">联系方式</div>
          <div class="md">:</div>
          <div class="rt">{{ item.phone }}</div>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: true,
      productOptions:[],//产品下拉框选项
      searchKey: "", // 搜索词
      codeImg: "http://wudao.cqlink.club/captcha?", //验证码图片
      workData: "",
      fileList: [],
      dialogImageUrl: "",
      dialogVisible: false,
      product: "", // 产品名称
      product_issue: "", // 产品问题
      remind: "", // 温馨提示
      detail: "", // 问题描述
      files: [], // 文件
      secret_files: [], // 机密文件
      phone: "", // 联系方式
      code:"",//验证码
    };
  },
  created () {
    this.$http.getConfig().then((res) => {
      if (res.data.code === 0) {
        this.remind = res.data.data.note;
      } else {
        this.$message.error('请求失败，请重试！')
      }
    })
    // 产品下拉选框
    this.$http.getChanpin().then((res) => {
      if (res.data.code === 0) {
        this.productOptions = res.data.data;
      } else {
        this.$message.error('请求失败，请重试！')
      }
    })
  },
  methods: {
    // 验证码刷新
    refresh() {
      var num = Math.ceil(Math.random() * 10); //生成一个随机数（防止缓存）
      this.codeImg = this.codeImg + num;
    },
    //添加附件
    uploadImg(param) {
      let fileObj = param.file;
      let form = new FormData();
      form.append("file", fileObj);
      this.$http.postUpload(form).then((res) => {
        // console.log(res.data);
        if (res.data.msg === "上传成功") {
          this.$message.success("上传成功！");
          this.files.push(res.data.data);
          return res.data.data;
        } else {
          this.$message.error("上传失败！");
        }
      });
    },
    //机密文件
    uploadFile(param) {
      let fileObj = param.file;
      let form = new FormData();
      form.append("file", fileObj);
      this.$http.postUpload(form).then((res) => {
        if (res.data.msg === "上传成功") {
          this.$message.success("上传成功！");
          this.secret_files.push(res.data.data);
        } else {
          this.$message.error("上传失败！");
        }
      });
    },
    // handleAvatarSuccessImg(response, file, fileList) {
    //   this.$message.success("上传成功！");
    //   console.log("response", response);
    //   console.log("file", file);
    //   console.log("fileList", fileList);
    //   this.files.push(response.data);
    // },
    // handleAvatarSuccessFile(response, file, fileList) {
    //   this.$message.success("上传成功！");
    //   console.log("response", response);
    //   console.log("file", file);
    //   console.log("fileList", fileList);
    //   this.secret_files.push(response.data);
    // },
    // handleAvatarError() {
    //   this.$message.error("上传失败！");
    // },
    handleExceedImg() {
      this.$message.error("最多只能上传4张图片！");
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 提交
    submite() {
      const params = new FormData();
      if (this.files.length > 0) {
        this.files.forEach((element) => {
          params.append("files[]", element);
        });
      } else {
        params.append("files[]", []);
      }
      if (this.secret_files.length > 0) {
        this.secret_files.forEach((element) => {
          params.append("secret_files[]", element);
        });
      } else {
        params.append("secret_files[]", []);
      }
      params.append("product", this.product);
      params.append("product_issue", this.product_issue);
      params.append("remind", this.remind);
      params.append("detail", this.detail);
      params.append("phone", this.phone);
      params.append("code", this.code);
      this.$http.postWork(params).then((res) => {
        if (res.data.code === 0) {
          this.$message({
            message: "提交成功！",
            type: "success",
          });
          location.reload();
        } else {
          this.$message.error(res.data.msg);
          this.refresh();
        }
      });
    },
    // 搜索
    search() {
      if (this.searchKey.trim().length <= 0) {
        this.$message.error("搜索词不能为空！");
      } else {
        this.$http.getWork(this.searchKey).then((res) => {
          // console.log(res.data.data);
          if (res.data.code === 0) {
            if (res.data.data.length > 0) {
              this.isShow = false;
              this.workData = res.data.data;
            } else {
              this.$message.error("没搜索到你想要的内容！");
              this.isShow = true;
            }
          } else {
            this.$message.error("请求失败，请重试！");
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.search {
  width: 636px;
  height: 44px;
  background: #ffffff;
  border-radius: 22px;
  overflow: hidden;
  display: flex;
  align-items: center;
  margin: 0 auto;
  margin-top: 28px;
  margin-bottom: 40px;
  input {
    width: 582px;
    height: 44px;
    border: none;
    outline: none;
    padding: 0 40px;
    font-size: 20px;
  }
  input::-webkit-input-placeholder {
    color: #cccccc;
    font-size: 20px;
  }
  input::-moz-placeholder {
    /* Mozilla Firefox 19+ */
    color: #cccccc;
    font-size: 20px;
  }
  input:-moz-placeholder {
    /* Mozilla Firefox 4 to 18 */
    color: #cccccc;
    font-size: 20px;
  }
  input:-ms-input-placeholder {
    /* Internet Explorer 10-11 */
    color: #cccccc;
    font-size: 20px;
  }
  img {
    width: 24px;
    height: 24px;
    cursor: pointer;
  }
}
.search-result {
  max-width: 1200px;
  margin: 0 auto;
  background: #ffffff;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
  border-radius: 10px;
  padding: 40px 80px;
  margin-bottom: 80px;
  .row {
    display: flex;
    .lf {
      width: 110px;
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      text-align-last: justify;
      text-align: justify;
      text-justify: distribute-all-lines; // 这行必加，兼容ie浏览器
    }
    .md {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-right: 10px;
    }
    .rt {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      line-height: 40px;
      color: #333333;
      width: 925px;
      img {
        width: 120px;
        height: 120px;
        border: 1px solid #707070;
      }
      img:not(:last-child) {
        margin-right: 20px;
      }
      a {
        display: block;
      }
    }
  }
  .row:not(:last-child) {
    margin-bottom: 30px;
  }
}
.word-order {
  max-width: 1200px;
  margin: 0 auto;
  background: #fff;
  padding: 0 200px;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.16);
  border-radius: 10px;
  padding-bottom: 108px;
  margin-bottom: 80px;
  .title {
    padding: 40px 0;
    text-align: center;
    font-size: 48px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #333333;
  }
  .row-input {
    display: flex;
    align-items: center;
    margin-bottom: 30px;
    .lf {
      width: 110px;
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      text-align-last: justify;
      text-align: justify;
      text-justify: distribute-all-lines; // 这行必加，兼容ie浏览器
    }
    .md {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-right: 10px;
    }
    .rt {
      width: 680px;
      height: 33px;
      line-height: 33px;
      border: 1px solid #707070;
      border-radius: 4px;
      padding: 0 10px;
    }
  }
  .row-textarea {
    display: flex;
    align-items: center;
    margin-bottom: 30px;
    .lf {
      width: 110px;
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      text-align-last: justify;
      text-align: justify;
      text-justify: distribute-all-lines; // 这行必加，兼容ie浏览器
    }
    .md {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-right: 10px;
    }
    .rt {
      width: 680px;
      height: 73px;
      border: 1px solid #707070;
      border-radius: 4px;
      resize: none;
      padding: 10px;
    }
  }
  .row-textarea.upload-file {
    align-items: flex-start;
  }
  .row-email,
  .row-security-code {
    display: flex;
    margin-bottom: 30px;
    align-items: center;
    .lf {
      width: 110px;
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      text-align-last: justify;
      text-align: justify;
      text-justify: distribute-all-lines; // 这行必加，兼容ie浏览器
    }
    .md {
      font-size: 24px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #999999;
      margin-right: 10px;
    }
    .rt {
      width: 200px;
      height: 33px;
      line-height: 33px;
      border: 1px solid #707070;
      border-radius: 4px;
      padding: 0 10px;
      margin-right: 10px;
    }
    .code-img{
      width: 120px;
      height: 50px;
    }
    span {
      font-size: 20px;
      font-family: PingFang SC;
      font-weight: 400;
      line-height: 40px;
      color: #536786;
      text-decoration: underline;
      cursor: pointer;
    }
  }
  .submit-btn {
    width: 306px;
    height: 88px;
    background: #536786;
    border: 1px solid #707070;
    border-radius: 4px;
    font-size: 48px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 88px;
    color: #ffffff;
    text-align: center;
    margin: 0 auto;
    cursor: pointer;
  }
  // scss中修改element样式
  ::v-deep {
    .upload .el-upload--picture-card {
      width: 120px;
      height: 120px;
      background: #ffffff;
      border: 1px solid #999999;
      border-radius: 4px;
      line-height: 120px;
    }
    .upload .el-upload-list--picture-card .el-upload-list__item {
      width: 120px;
      height: 120px;
      border: 1px solid #999999;
    }
    // 隐藏删除预览
    .el-upload-list__item-actions,
    .el-icon-close,
    .el-icon-close-tip {
      display: none;
    }
  }
}
::v-deep{
  .el-input--suffix .el-input__inner{
  border-color: #707070;
}
}
</style>
